﻿<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 私信控制器
 */
class Chat extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('bbs_user_model');
        $this->load->model('bbs_dialog_model');
    }
    
    /**
     * 私信对话框
     * @param integer $user_id 用户加密id
     * @param string $dialog_to 聊天对象唯一标识
     */
    function index() {
        $user_id = isset($_REQUEST['user_id']) ? decrypt_user_id(trim($_REQUEST['user_id'])) : 0;
        $dialog_to = isset($_REQUEST['dialog_to']) ? trim($_REQUEST['dialog_to']) : "";
        
        $dialog_from = $this->db->query("select head_img from tan_users_info where user_id = $user_id order by id desc limit 1")->row_array();
        
        $dialog_user = $this->bbs_user_model->get_user_by_condition(array("salt"=>$dialog_to));
        //获取聊天室
        $dialog_room = $this->bbs_dialog_model->get_dialog_room(array("dialog_from"=>$user_id,"dialog_to"=>$dialog_user['user_id']));
        $message_list = array();
        if($dialog_room){
            $message_list = $this->bbs_dialog_model->get_message_list("((sender_to_id = ".$user_id." AND is_show = 0) or (sender_from_id = ".$user_id.")) AND room_name = '".$dialog_room['room_name']."'",0,10);
            //更新所有私信为已读
            $this->bbs_dialog_model->update_message(array("sender_from_id"=>$dialog_user['user_id'],"sender_to_id"=>$user_id),array("is_read"=>1));
        }else{
            //创建聊天室
            $room_name = random_string('alnum', 6);
            $dialog_room = array(
                0=>array("room_name"=>$room_name,"dialog_from"=>$user_id,"dialog_to"=>$dialog_user['user_id'],"is_del"=>0,"is_show"=>0),
                1=>array("room_name"=>$room_name,"dialog_from"=>$dialog_user['user_id'],"dialog_to"=>$user_id,"is_del"=>0,"is_show"=>0)
            );
            $this->bbs_dialog_model->insert_dialog_room($dialog_room);
        }
        if($message_list){
            $message_list = $this->arr_to_sort_by_create_time($message_list);
        }
        foreach ($message_list as $key=>$val){
            $message_list[$key]['time_mark'] = $val['time_mark'] ? date("Y-m-d H:i:s",$val['time_mark']) : 0;
            $message_list[$key]['is_self'] = $val['sender_from_id'] == $user_id ? 1 : 0;
        }
        $this->assign("message_list", $message_list);
        $this->assign("to_user",  json_encode(array("uuid"=>encrypt_user_id($dialog_user['user_id']),"headImg"=>$dialog_user['head_img'])));
        $this->assign("from_user",  json_encode(array("uuid"=>encrypt_user_id($user_id),"headImg"=> $dialog_from['head_img'])));
        $this->assign("from_user_headimg",  $dialog_from['head_img']);
        $this->assign("to_user_headimg",  $dialog_user['head_img']);
        $this->display('chat/index.html');
    }
    
    //聊天信息（获取，发送）
    function chat_message(){
        
        $user_id = isset($_REQUEST['fromId']) ? decrypt_user_id(trim($_REQUEST['fromId'])) : 0;
        $dialog_to = isset($_REQUEST['toId']) ? decrypt_user_id(trim($_REQUEST['toId'])) : 0;
        $since_timestamp = isset($_REQUEST['since_timestamp']) ? trim($_REQUEST['since_timestamp']) : 0;
        
        //获取聊天室
        $dialog_room = $this->bbs_dialog_model->get_dialog_room(array("dialog_from"=>$dialog_to,"dialog_to"=>$user_id));
        //查询更多
        if($since_timestamp){
            $message_list = $this->bbs_dialog_model->get_message_list("((sender_to_id = ".$user_id." AND is_show = 0) or (sender_from_id = ".$user_id.")) AND room_name = '".$dialog_room['room_name']."' and create_time < $since_timestamp",0,10);
            if($message_list){
                $message_list = $this->arr_to_sort_by_create_time($message_list);
            }
            foreach ($message_list as $key=>$val){
                $message_list[$key]['time_mark'] = $val['time_mark'] ? date("Y-m-d H:i:s",$val['time_mark']) : 0;
                $message_list[$key]['is_self'] = $val['sender_from_id'] == $user_id ? 1 : 0;
            }
            
            $mess['data'] = array(
                "count" => count($message_list),
                "list" => $message_list,
            );
            echo json_encode($mess);die;
        }
    }
    
    private function arr_to_sort_by_create_time($arr){
        $create_time_arr = array();
        foreach ($arr as $key =>$val){
            $create_time_arr[$key] = $val['create_time'];
        }
        array_multisort($create_time_arr,SORT_ASC,$arr);
        return $arr;
    }
}
